package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Autowired
    private ItemMapper itemMapper;

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageDTO<Item> list(Integer page, Integer size) {
        //创建一个page对象，设置当前页和每页大小
        Page<Item> ppage = new Page<>(page, size);
        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
        Page<Item>itemPage =itemMapper.selectPage(ppage,wrapper);
        PageDTO<Item> pageDTO = new PageDTO<>();
        pageDTO.setTotal(itemPage.getTotal());
        pageDTO.setList(itemPage.getRecords());
        return pageDTO;
    }

    /**
     * 根据ID查询商品
     * @param id
     * @return
     */
    @Override
    public Item getById(Long id) {
        //调用mapper层方法，根据id查询商品
        return itemMapper.selectById(id);
    }

}
